/*
 Copyright 2008 Under Dusken

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package org.dyndns.anderlin.jmplayer.client.ui.wizard;

import org.dyndns.anderlin.jmplayer.model.JmplayerObject;
import org.dyndns.anderlin.jmplayer.service.GenericService;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.ApplicationContext;
import org.springframework.richclient.application.event.LifecycleApplicationEvent;
import org.springframework.richclient.command.ActionCommandExecutor;
import org.springframework.richclient.form.CompoundForm;
import org.springframework.richclient.wizard.AbstractWizard;
import org.springframework.richclient.wizard.WizardDialog;

/**
 * Generic wizard
 */
public abstract class GenericWizard<T extends JmplayerObject> extends AbstractWizard implements ActionCommandExecutor {

    private WizardDialog wizardDialog;

    private CompoundForm wizardForm;

    private GenericService<T> genericService;

    public GenericWizard(String name) {
        super(name);
    }

    @Required
    public void setGenericService(GenericService<T> genericService) {
        this.genericService = genericService;
    }

    protected CompoundForm getWizardForm() {
        return wizardForm;
    }

    /**
     * Add the pages this wizard consists of
     */
    public abstract void addPages();

    /**
     * Create the default object to start editing
     *
     * @return an object ready for editing
     */
    protected abstract T getObject();

    public void execute() {

        T entity = getObject();

        if (entity == null) {
            // do nothing, it might have been cancelled by the user
            return;
        }

        if (wizardDialog == null) {
            wizardForm = new CompoundForm();
            wizardForm.setFormObject(getObject());
            wizardDialog = new WizardDialog(this);
        }
        wizardDialog.showDialog();
    }

    protected boolean onFinish() {
        T entity = getNewEntity();
        T newEntity = genericService.saveOrUpdate(entity);
        wizardDialog = null;
        ApplicationContext ac = getApplicationContext();
        if (ac != null) {
            if (entity.getID() == null) {
                ac.publishEvent(new LifecycleApplicationEvent(
                        LifecycleApplicationEvent.CREATED, newEntity));
            } else {
                ac.publishEvent(new LifecycleApplicationEvent(
                        LifecycleApplicationEvent.MODIFIED, newEntity));
            }
        }
        return true;
    }

    private T getNewEntity() {
        wizardForm.commit();
        //noinspection unchecked
        return (T) wizardForm.getFormObject();
    }

}